﻿using AutoMapper;
using MediatR;
using Traceability.Commont.Domains.AnimalsDomainFiles;
using Traceability.Commont.DTOs.CommontFiles;
using Traceability.Commont.Infrastructures.Interfaces;

namespace U.Enterprise.Api.Applications.BatchManagements
{
    public class SlaughterCraeteHander : IRequestHandler<SlaughterCraeteCommand, Resqults>
    {
        private readonly IBateRepository<SlaughteredDetails> slaig;
        private readonly IMapper mapper;

        public SlaughterCraeteHander(IBateRepository<SlaughteredDetails> slaig, IMapper mapper)
        {
            this.slaig = slaig;
            this.mapper = mapper;
        }

        public async Task<Resqults> Handle(SlaughterCraeteCommand request, CancellationToken cancellationToken)
        {
            try
            {
                Resqults res=new Resqults() { Code=0};
                SlaughteredDetails slaug = new SlaughteredDetails();
                slaug.ApproachId = request.ApproachId;
                slaug.ApproachNo = request.ApproachNo;
                slaug.Suppliers = request.Suppliers;
                slaug.ApproachBatch = request.ApproachBatch;
                slaug.SlaughterCircleNum = request.SlaughterCircleNum;
                slaug.ShoppingDesignationid = request.ShoppingDesignationid;
                slaug.AverageWeight = request.AverageWeight;
                slaug.SlaughterCircleId = request.SlaughterCircleId;
                slaug.Weight = request.Weight;
                slaug.Remark = request.Remark;
                slaug.WeighTime = request.WeighTime;
                slaug.Status = request.Status;
                if (request.Pid == 0)
                {
                    res.Code = await slaig.Add(slaug);
                    if (res.Code > 0)
                    {
                        res.Messges = "成功";
                    }
                }
                else
                {
                    res.Code=await slaig.Update(slaug);
                    if (res.Code > 0)
                    {
                        res.Messges = "移圈成功";
                    }
                }
                return res;
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
